﻿Namespace SRAGWM

    Public Class GWMHelper

        Shared Sub Connection(ByVal objDB As Database, ByVal MapServer As SRAMapServer, _
                              ByVal pCoordinateSystem As String, _
                              ByRef objMapServer As MAPSRVLib.GWMMapServerHelper)

            Dim MapServerManager As New SRAMapServerBuilder

            objMapServer = MapServerManager.GetMap()
            objMapServer.Clear()
            objMapServer.OutputType = MapServer.OutputType
            objMapServer.BackgroundColor = RGB(MapServer.BackgroundColor.R, _
                                               MapServer.BackgroundColor.G, _
                                               MapServer.BackgroundColor.B)

            objMapServer.SetCoordinateSystem(pCoordinateSystem)

            objMapServer.Connect(objDB.DBType, objDB.DBName, objDB.Source, objDB.ConnectionName)

        End Sub

        Shared Sub GenerateMap(ByRef objMapServer As MAPSRVLib.GWMMapServerHelper, ByVal objRange As Range, _
                            ByVal objMapAtributes As MapAtributes)

            Dim AS1 = Nothing
            If (objMapServer.AutoScales.Count = 0) Then
                AS1 = objMapServer.AddRangeAutoScale("AS1")
            Else
                AS1 = objMapServer.AutoScales("AS1")
            End If

            AS1.Multiplier = 1 * objRange.MagFact
            AS1.MaximumWindow = objRange.Maximum
            AS1.MinimumWindow = objRange.Minimum

            objMapServer.CreateMapByRange(objMapAtributes.Rng(0), objMapAtributes.Rng(1), objMapAtributes.Rng(2), _
                                          objMapAtributes.Rng(3), objMapAtributes.Width, objMapAtributes.Height, _
                                          "AS1", AS1)

            'objMapServer.CreateMapByRange(objMapAtributes.Rng(0) + dx * dx1, _
            '                              objMapAtributes.Rng(1) + dy * dy1, _
            '                              objMapAtributes.Rng(2) + dx * dx2, _
            '                              objMapAtributes.Rng(3) + dy * dy2, _
            '                              objMapAtributes.Width, objMapAtributes.Height, _
            '                              "AS1", AS1)

        End Sub

        Shared Function DisplayFeature(ByRef objMapServerHelper As MAPSRVLib.GWMMapServerHelper, _
                                ByRef objLegendEntry As SRAGWM.LegendEntry, ByVal Tooltip As String)

            Dim objMarker As MAPSRVLib.GWMMarkerSystem
            Dim objDisplayRule As MAPSRVLib.GWMDisplayRule
            Dim objSymbology As MAPSRVCOMPONENTSLib.GWMFeatureSymbology

            objMarker = objMapServerHelper.AddMarker(objLegendEntry.DataSource.DBName, objLegendEntry.DataSource.DB, _
                                     objLegendEntry.DataSource.Table, "", objLegendEntry.DataSource.Atributes)

            objMarker.AddElementsByPolygon(objLegendEntry.Filter.WhereClause, objLegendEntry.Filter.Points, _
                                        objLegendEntry.Filter.NumPairs, objLegendEntry.Filter.SpatialOperator)

            objDisplayRule = objMarker.NewDisplayRule
            objSymbology = objMapServerHelper.CreateObject("GMWebMap.GWMFeatureSymbology")
            objSymbology.Color = RGB(objLegendEntry.BorderColor.R, _
                                     objLegendEntry.BorderColor.G, _
                                     objLegendEntry.BorderColor.B)

            objSymbology.Weight = objLegendEntry.SymbologyWeight
            objSymbology.Style = objLegendEntry.SymbologyStyle
            objSymbology.FillColor = RGB(objLegendEntry.FillColor.R, _
                                         objLegendEntry.FillColor.G, _
                                         objLegendEntry.FillColor.B)

            objDisplayRule.DisplaySymbology = objSymbology
            objDisplayRule.Tooltip = objLegendEntry.Tooltip

            'Return to Rng
            Return objMarker.Range

        End Function
    End Class
End Namespace
